//
//  UIButton+Extension.swift
//  新浪微博
//
//  Created by WTFKL on 17/8/5.
//  Copyright © 2017年 WTF. All rights reserved.
//

import UIKit


extension UIButton {
    
    enum ImageOrBgImage {
        case foreImage
        case bgImage
        case both
    }
    
    
    /// 便捷Button初始化方法
    ///
    /// - Parameters:
    ///   - title: button的显示文字
    ///   - imageName: button图片的名字，如果为空，默认为nil
    ///   - state: 按钮状态，如果为空默认为normal
    ///   - setForImageOrBgImage: 设置image或者backgroundImage，传入 foreImage 设置image，bgImage设置backgroundImage,both为两者设置
    /// - Returns: 返回一个按钮
    func createCustomButton(title:String? , imageName : String? , state : UIControlState? , setForImageOrBgImage : ImageOrBgImage ) -> UIButton {
        let cstButton = UIButton();
        let image = UIImage(named: imageName ?? "" );
        cstButton.setTitle(title, for: state ?? .normal);
        
        if setForImageOrBgImage == ImageOrBgImage.foreImage {
            cstButton.setImage(image, for: state ?? .normal);
        }else if setForImageOrBgImage == ImageOrBgImage.bgImage{
            cstButton.setBackgroundImage(image, for: state ?? .normal);
        }else if setForImageOrBgImage == ImageOrBgImage.both{
            cstButton.setImage(image, for: state ?? .normal);
            cstButton.setBackgroundImage(image, for: state ?? .normal);
        }
        
        
        
        return cstButton;
    }
    
    convenience init(title : String? , normalColor : UIColor? , highLightColor : UIColor? ,bgColor : UIColor? ,normalImage: UIImage) {
        self.init();
        self.setTitle(title, for: .normal);
        self.setTitleColor(normalColor ?? #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal);
        self.setTitleColor(highLightColor ?? #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) , for: .highlighted);
        self.setBackgroundImage(normalImage, for: .normal);
//        self.backgroundColor = bgColor ?? UIColor.white;
        
    }
    
    
    
}
